package cn.zhiyong.pr.main;

import java.util.ArrayList;
import java.util.List;

import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.StyledDocument;

import org.hibernate.Query;
import org.hibernate.Session;

import cn.zhiyong.pr.database.Group;
import cn.zhiyong.pr.database.HibernateUtil;

public class MainData {
	StyledDocument currentDoc=new DefaultStyledDocument();
	final ArrayList<PrdocModel> searchresult;
	final ArrayList<String> allGroups;

	// ***end of definition

	public MainData() {
		searchresult = new ArrayList<PrdocModel>();
		allGroups = new ArrayList<String>();
		init();
	}

	private void init() {
		// load all groups from DB
		Session s = HibernateUtil.getSessionFactory().getCurrentSession();
		s.beginTransaction();
		Query q = s.createQuery("from Group");
		List groupsFromDB = q.list();
		for (Object o : groupsFromDB) {
			Group g = (Group) o;
			String aGroupName = g.getName();
			if (!allGroups.contains(aGroupName)) {
				allGroups.add(aGroupName);
			}
		}
		s.getTransaction().commit();

	}

	public void updateGroupsFromDB() {
		allGroups.clear();
		init();
	}
	
	public StyledDocument getCurrentDoc() {
		return currentDoc;
	}

	public void setCurrentDoc(StyledDocument currentDoc) {
		this.currentDoc = currentDoc;
	}

	public ArrayList<PrdocModel> getSearchresult() {
		return searchresult;
	}

	public ArrayList<String> getAllGroups() {
		return allGroups;
	}

}
